package com.scut.dispatching_server.service;

import com.scut.dispatching_server.config.GlobalVariables;
import com.scut.dispatching_server.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StatisticsService {
    @Autowired
    private RedisUtil redisUtil;

    public void addAnswerTime(int answerTime) {
        redisUtil.lSet(GlobalVariables.ROUND_ANSWER_TIME_LIST, answerTime);
    }

    public void addSuccessDemands(String demandId) {
        redisUtil.lSet(GlobalVariables.ROUND_SUCCESS_DEMAND_LIST, demandId);
    }

    public void incrDemandCount() {
        redisUtil.incr(GlobalVariables.DEMAND_COUNT, 1);
    }

    public void incrExpiredDemandCount() {
        redisUtil.incr(GlobalVariables.EXPIRED_DEMAND_COUNT, 1);
    }

    public void incrSuccessDemandCount() {
        int listSize = (int)redisUtil.lGetListSize(GlobalVariables.ROUND_SUCCESS_DEMAND_LIST);
        redisUtil.incr(GlobalVariables.SUCCESS_DEMAND_COUNT, listSize);
    }

    public void addAvgAnswerTimeToAnswerTimeList() {
        int listSize = (int)redisUtil.lGetListSize(GlobalVariables.ROUND_ANSWER_TIME_LIST);
        if (listSize == 0) {
            redisUtil.lSet(GlobalVariables.AVG_ANSWER_TIME_LIST, 0);
            return;
        }
        List<Object> answerTimeList = redisUtil.lGet(GlobalVariables.ROUND_ANSWER_TIME_LIST, 0, listSize);
        int sum = 0;
        for (Object answerTime : answerTimeList) {
            sum += (Integer) answerTime;
        }
        redisUtil.lSet(GlobalVariables.AVG_ANSWER_TIME_LIST, (float) sum / listSize);
        redisUtil.del(GlobalVariables.ROUND_ANSWER_TIME_LIST);
    }

    public void addSuccessRateToSuccessRateList() {
        int listSize = (int)redisUtil.lGetListSize(GlobalVariables.ROUND_SUCCESS_DEMAND_LIST);
        int roundDemandNum = (int) redisUtil.get(GlobalVariables.ROUND_DEMAND_COUNT);
        if (roundDemandNum == 0) {
            redisUtil.lSet(GlobalVariables.SUCCESS_RATE_LIST, 0);
            return;
        }
        redisUtil.lSet(GlobalVariables.SUCCESS_RATE_LIST, (float) listSize / roundDemandNum);
        redisUtil.del(GlobalVariables.ROUND_SUCCESS_DEMAND_LIST);
    }

    public void setRoundDemandNum(int roundDemandNum) {
        redisUtil.set(GlobalVariables.ROUND_DEMAND_COUNT, roundDemandNum);
    }

    public void reCaculateAvgAnswerTime() {
        int successDemandNum = (int)redisUtil.get(GlobalVariables.SUCCESS_DEMAND_COUNT);
        if (successDemandNum == 0) {
            redisUtil.set(GlobalVariables.AVG_ANSWER_TIME, 0);
            return;
        }
        int listSize = (int)redisUtil.lGetListSize(GlobalVariables.ROUND_SUCCESS_DEMAND_LIST);
        List<Object> answerTimeList = redisUtil.lGet(GlobalVariables.ROUND_ANSWER_TIME_LIST, 0, listSize);
        int sum = 0;
        for (Object answerTime : answerTimeList) {
            sum += (Integer) answerTime;
        }
        redisUtil.set(GlobalVariables.AVG_ANSWER_TIME, (sum + successDemandNum * Float.parseFloat(redisUtil.get(GlobalVariables.AVG_ANSWER_TIME).toString())) / (successDemandNum + listSize));
    }
}
